#include "parse.h"



OverviewPage * testParseLoginResponse(){
	//char * test ="9\n98e2f3401441\nflight owndeploy\n5\n[2:471:6]\n[2:487:7]\n5:34:55\n08/11 04:23:16\nSmall Cargo 2\n";
	//char * test ="9\n98e2f3401441\nflight owndeploy\n6\n[2:471:6]\n[2:487:7]\n5:34:55\n08/11 04:23:16\nSmall Cargo 2\nBig cargo 3\n";
	//char * test ="18\n98e2f3401441\nflight owndeploy\n6\n[2:471:6]\n[2:487:7]\n5:34:55\n08/11 04:23:16\nSmall Cargo 2\nBig cargo 3\nflight attack\n7\n[2:471:6]\n[2:487:7]\n5:34:55\n08/11 14:23:16\nSmall Cargo 2\nBig cargo 3\nBattle ship 5\n";

	//char * test ="23\n383a6c15d37e\nflight owntransport\n6\n[2:487:7]\n[2:471:6]\n1:04:32\n08/11 20:58:16\nSmall Cargo 1\nreturn owntransport\n6\n[2:487:7]\nTransport\n2:11:36\n08/11 22:05:20\nSmall Cargo 1\nflight owndeploy\n6\n[2:471:6]\n[2:487:7]\n5:32:12\n09/11 01:25:56\nSmall Cargo 1\n";
	//char * test ="9\n75ef4ef9e1dc\nflight owndeploy\n6\n. Its mission is:\ntu\n3:22:42\n10/11 20:42:40\nSmall Cargo 1\n";
	//const char * test ="9\n538059250c04\nflight owndeploy\n6\n[2:471:6]\n[2:421:10]\n2:37:55\n11/11 02:57:48\nLight Fighter 1\n";
	const char * test ="16\n538059250c04\nflight owndeploy\n6\n[2:471:6]\n[2:421:10]\n2:37:55\n11/11 02:57:48\nLight Fighter 1\nflight owndeploy\n6\n[2:470:6]\n[2:420:10]\n2:37:50\n11/10 02:57:48\nHeavy Fighter 1\n";
	int l = strlen(test);
	char * testd = (char *) malloc( l * sizeof(char));
	strcpy(testd,test);
	OverviewPage * op;
	op = parseLoginResponse(testd);
	free(testd);
	return op;
}

void parsev2(char * buf){
	NF_WriteText(0, 0, 0, 0, "parsingtest       ");
	char * pch;
	int i = 1;
	pch = strtok (buf,"\n");
	while( pch != NULL){
		pch = strtok (NULL, "\n");
		NF_WriteText(0, 0, 0, i, pch);
		NF_UpdateTextLayers();
		i++;
	}
}

OverviewPage * parseLoginResponse(char * buf){

	/*OverviewPage  * ovp1 = new OverviewPage();
	ovp1->setSession("testsession");
	Fleet * f1 = new Fleet();
	(*f1).setMission("mission");
	(*f1).setSource("source");
	ovp1->addFleet(*f1);
	return ovp1;*/
	//nocashMessage("test");
	/*parsev2(buf);
	return NULL;*/
	
	OverviewPage  * ovp = new OverviewPage();
	NF_WriteText(0, 0, 0, 0, "parsing             ");
	char * pch;
	//NF_WriteText(0, 0, 0, 0, txtbuff);
	pch = strtok (buf,"\n");
	char  * nb = (char*) malloc(strlen(pch) * sizeof(char));
	strcpy(nb,pch);
	int nbline = atoi(nb);
	NF_WriteText(0, 0, 0, 1, "                     ");
	NF_WriteText(0, 0, 0, 1, nb);
	NF_UpdateTextLayers();
	free(nb);

	pch = strtok (NULL, "\n");
	char  * session = (char*) malloc(strlen(pch) * sizeof(char));	
	strcpy(session,pch);
	
	NF_WriteText(0, 0, 0, 2, "                     ");
	NF_WriteText(0, 0, 0, 2, session);
	NF_UpdateTextLayers();
	ovp->setSession(session);
	free(session);

	int i = 2;
	Fleet * f;
	int pass= 0;
	char tmpbuf[32];
	while( i < nbline && pch != NULL){
		/*NF_WriteText(0, 0, 0, 3 + pass, "passage");
		NF_UpdateTextLayers();
		pass++;*/
	
		f = new Fleet();
		pch = strtok (NULL, "\n");
		char  * mission = (char*) malloc(strlen(pch) * sizeof(char));
		strcpy(mission,pch);
		i++;
		f->setMission(mission);
		free(mission);
		
		
		pch = strtok (NULL, "\n");
		char  * nbl = (char*) malloc(strlen(pch) * sizeof(char));
		strcpy(nbl,pch);
		i++;
		
		pch = strtok (NULL, "\n");
		char  * source = (char*) malloc(strlen(pch) * sizeof(char));
		strcpy(source,pch);
		i++;
		f->setSource(source);
		free(source);


		pch = strtok (NULL, "\n");
		char  * dest = (char*) malloc(strlen(pch) * sizeof(char));
		strcpy(dest,pch);
		i++;
		f->setDestination(dest);
		free(dest);


		pch = strtok (NULL, "\n");
		char  * sl = (char*) malloc(strlen(pch) * sizeof(char));
		strcpy(sl,pch);
		i++;
		f->setSecondleft(sl);
		free(sl);

		pch = strtok (NULL, "\n");
		char  * it = (char*) malloc(strlen(pch) * sizeof(char));
		strcpy(it,pch);
		i++;
		f->setImpacttime(it);
		free(it);

		int nbv = atoi(nbl);
		nbv = nbv -5;
		

		/*for(int j = 0 ; j<300; j++)
			swiWaitForVBlank();*/

		
		/*sprintf(tmpbuf,"nbv = %d", nbv);
		NF_WriteText(0, 0, 0, 3 + pass, nbl);
		NF_UpdateTextLayers();
		pass++;*/
			
			
		while( nbv > 0){
			pch = strtok (NULL, "\n");
			char  * ship = (char*) malloc(strlen(pch) * sizeof(char));
			strcpy(ship,pch);
			string tmp = ship;
			i++;
			f->addShip(tmp);
			nbv--;

			
		}
		ovp->addFleet(*f);
		delete f;
		f= NULL;

	}

	return ovp;



}
/*
28
f546a76011ac
flight owndeploy
18
[9:293:4]
[9:293:5]
4:34:53
16/11 06:15:24
Petit transporteur 4.432
Grand transporteur 10.785
Chasseur lÃ©ger 5
Chasseur lourd 226
Croiseur 168
Vaisseau de bataille 1.600
Vaisseau de colonisation 14
Recycleur 1.202
Sonde espionnage 5.788
Bombardier 520
Destructeur 340
Ã‰toile de la mort 1
Traqueur 159
flight owndeploy
6
[6:182:4]
[9:293:4]
4:56:15
16/11 06:36:46
Grand transporteur 377
*/
